package com.example.exampleproject.net;

import android.content.Context;

import com.example.exampleproject.cache.NetCacheManager;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class CacheInterceptor implements Interceptor {

    private Context context;

    public CacheInterceptor(Context context) {
        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        String cacheResponse = response.body().string();
        NetCacheManager.netCache("weather", cacheResponse);
        Response newResponse = response.newBuilder()
                .body(ResponseBody.create(MediaType.parse("application/json"), cacheResponse))
                .build();
        return newResponse;
    }

}
